<template>
  <div v-if="show" class="login-content body-bg">
    <button
      v-if="canIUse"
      type="primary"
      open-type="getPhoneNumber"
      @getphonenumber="getPhoneNumber"
    >
      <img src="/static/images/WeChat.png" alt="wechat" /> 使用微信授权登录
    </button>
    <p v-else>请升级微信版本</p>
  </div>
</template>

<script>
import Ls from '@/utils/storage'
export default {
  data() {
    return {
      show: false,
      canIUse: wx.canIUse("button.open-type.getPhoneNumber"),
    };
  },
  onLoad() {
    // 仅用于获取不到基本id时候做本地开发使用，测试和线上请注释
    Ls.setItem("merchant_id","121d698b6c4b4034b94b515b24ab50c8")
    Ls.setItem("project_id","ff4212ae0fb24ba4aa3057b9d62ecd5f")
    Ls.setItem("platform_id","f7a3d19f52d6c5ab7841c7b82f03805a")
    Ls.setItem("parentInfor",{parent_id: "ff4212ae0fb24ba4aa3057b9d62ecd5f", parent_photo: "https://img.yzcdn.cn/vant/cat.jpeg", parent_name: "王大力", parent_phone: "15149337899"})
    
    let that = this;
    wx.checkSession({
      success() {
        console.log("checkSession true");
        //session_key 未过期，并且在本生命周期一直有效
        that.show = false;
        wx.switchTab({
          url: "/pages/index/main"
        });
      },
      fail() {
        console.log("checkSession false");
        // session_key 已经失效，需要重新执行登录流程
        that.show = true;
        wx.setNavigationBarTitle({
          title: "手机号登录"
        });
      }
    });
  },
  methods: {
    // 授权手机号登录
    getPhoneNumber(e) {
      console.log(e);
      console.log(e.mp.detail.iv);
      console.log(e.mp.detail.encryptedData);
      let that = this;
      this.show = false;
      this.getStudentList();
    },
    // 获取孩子列表
    getStudentList() {
      this.$fetch
        .getStudentList({
          is_default: ''
        }).then(res => {
          if(!res.result) {
            wx.redirectTo({
              url: '/pages/addChild/main?from=add'
            })
            return
          }
          // 时候有默认孩子，有则去默认孩子信息，否则去第一个孩子信息
          let defaultList = res.result.filter(item=> item.is_default == '1')
          if(defaultList.length){
            Ls.setItem("childInfor", {
              child_name: defaultList[0].name,
              child_photo: defaultList[0].avatar,
              student_id: defaultList[0].id,
              birthday: defaultList[0].birthday,
              birthday: defaultList[0].birthday,
              is_new_old_student: defaultList[0].is_new_old_student,
              is_default: defaultList[0].is_default,
              student_school_name: defaultList[0].student_school_name,
              student_school_id: defaultList[0].student_school_id,
            });
            wx.switchTab({
              url: "/pages/index/main"
            });
            return
          }
          Ls.setItem("childInfor", {
            child_name: res.result[0].name,
            child_photo: res.result[0].avatar,
            student_id: res.result[0].id,
            birthday: res.result[0].birthday,
            birthday: res.result[0].birthday,
            is_new_old_student: res.result[0].is_new_old_student,
            is_default: res.result[0].is_default,
            student_school_name: res.result[0].student_school_name,
            student_school_id: res.result[0].student_school_id,
          });
          wx.switchTab({
            url: "/pages/index/main"
          });
        })
        .catch(err => {

        });
    }
  }
};
</script>

<style lang="less">
.login-content {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  ._button {
    display: flex;
    justify-content: center;
    align-items: center;
    img {
      width: 34px;
      height: 34px;
      margin-right: 6px;
    }
  }
}
</style>
